/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import dbase.DBanggota;
import dbase.DBconnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.*;
import page.GoIndex;
import page.goRegistrasi;
import page.GoLihat;
import page.GoKendaraan;
import page.GoLogin;
import page.Goindex2;
/**
 *
 * @author aa
 */
public class controller extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, Exception {
        String page = request.getParameter("page");

        DBconnection dbConn = new DBconnection();
       List<anggota> category = new ArrayList<anggota>();
        DBanggota dbCat = new DBanggota(dbConn.getConnection());
        try{
            category = dbCat.getAllAnggota();
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            dbConn.closeConnection();
        }
        request.setAttribute("categories", category);

        RequestDispatcher dis = null;
        if(page != null){
            if(page.equals("index")){
                GoIndex index = new GoIndex(request);
                dis = request.getRequestDispatcher(index.getPage());
            } else if(page.equals("Lihat")){
                GoLihat cart = new GoLihat(request);
                dis = request.getRequestDispatcher(cart.getPage());
            } else if(page.equals("LihatKendaraanAll")){
                GoLihat cart = new GoLihat(request);
                dis = request.getRequestDispatcher(cart.getPage2());
            }  else if(page.equals("List")){
                GoLihat cart = new GoLihat(request);
                dis = request.getRequestDispatcher(cart.getPageKendaraan());
            }else if(page.equals("login")){
                GoLogin cart = new GoLogin(request);
                dis = request.getRequestDispatcher(cart.GoLogin());
            }else if(page.equals("Logout")){
                GoLogin cart = new GoLogin(request);
                dis = request.getRequestDispatcher(cart.GoLogin());
            }else if(page.equals("List2")){
                GoLihat cart = new GoLihat(request);
                dis = request.getRequestDispatcher(cart.deleteAnggota());
            }else if(page.equals("register")){
                dis = request.getRequestDispatcher("register.jsp");
            }else if(page.equals("tes")){
                dis = request.getRequestDispatcher("tesSession.jsp");
            }else if(page.equals("editAnggota")){
                dis = request.getRequestDispatcher("editAnggota.jsp");
            } else if(page.equals("Grafik")){
                dis = request.getRequestDispatcher("grafik.jsp");
            } else if(page.equals("Daftar")){
                goRegistrasi register = new goRegistrasi(request);
                GoIndex index = new GoIndex(request);
                dis = request.getRequestDispatcher(index.getPage());
            }else if(page.equals("DaftarKendaraan")){
                GoKendaraan register = new GoKendaraan(request);
                Goindex2 index = new Goindex2(request);
                dis = request.getRequestDispatcher(index.getPage());
            }
        } else {
            dis = request.getRequestDispatcher("index.jsp");
        }
        dis.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
